#include <WinTen.h>
#include <Windows.h>
#include <extend.h>

#define FD_HEIGHT    22
#define FD_BORDER     4

#define CLR_BLACK           0
#define CLR_GRAY      8421504

#define SETBLACKPEN( hDC )  SelectObject( hDC, GetStockObject( BLACK_PEN ) )
#define SETWHITEPEN( hDC )  SelectObject( hDC, hWhitePen )

//----------------------------------------------------------------------------//

CLIPPER FOLDPAINT() // ( hWnd, hDC, aPrompts, hFont1, hFont2,
                    //   nClrPane, nOption, aEnable, nSize, lWin95 ) --> nil
{
   HWND hWnd      = ( HWND ) _parni( 1 );
   HDC hDC        = ( HDC ) _parni( 2 );
   HPEN hDarkPen  = CreatePen( PS_SOLID, 1, GetSysColor( COLOR_BTNSHADOW ) );
   HPEN hWhitePen = CreatePen( PS_SOLID, 1, GetSysColor( COLOR_BTNHIGHLIGHT ) ); // RGB( 128, 128, 128 ) );
   WORD wPrompts  = _ALen( _param( 3, -1 ) );
   HFONT hFont1   = ( HFONT ) _parni( 4 );
   HFONT hFont2   = ( HFONT ) _parni( 5 );
   LONG rgbPane   = _parnl( 6 );
   WORD wOption   = _parni( 7 );
   WORD wSize     = _parni( 9 );
   BOOL bWin95    = _parl( 10 );
   WORD wWidth, wHeight, wLeft = 0, w ;
   RECT rct;
   POINT pt;
   SIZE size;

   if( ! wPrompts )
      return;

   GetWindowRect( hWnd, &rct );
   wHeight = rct.bottom - rct.top;
   wWidth  = rct.right - rct.left;

   if( ! wWidth )
      return;

   if( ! wSize )
      wSize = wWidth / wPrompts;

   if( bWin95 )
   {
      SelectObject( hDC, hFont1 );
      GetTextExtentPoint32( hDC, _parc( 3, 1 ), _parclen( 3, 1 ), &size );
      wSize = size.cx + 12;
   }

   if( ! bWin95 )
   {
      SETBLACKPEN( hDC );
      MoveToEx( hDC, 0, wHeight, &pt );
      LineTo( hDC, 0, 5 );
   }

   SETWHITEPEN( hDC );
   if( ! bWin95 )
   {
      MoveToEx( hDC, 1, wHeight - 1, &pt );
      LineTo( hDC, 1, 5 );
      MoveToEx( hDC, 2, wHeight - 1, &pt );
      LineTo( hDC, 2, FD_HEIGHT + 1 );
   }
   else
   {
      MoveToEx( hDC, 0, wHeight - 1, &pt );
      LineTo( hDC, 0, 2 + IF( wOption != 1, 2, 0 ) );
   }

   SETBLACKPEN( hDC );
   MoveToEx( hDC, 0, wHeight - 1, &pt );
   LineTo( hDC, wWidth, wHeight - 1 );

   SETBLACKPEN( hDC );
   MoveToEx( hDC, wWidth - 1, wHeight - 1, &pt );
   LineTo( hDC, wWidth - 1, FD_HEIGHT - 1 );

   SelectObject( hDC, hDarkPen );
   if( ! bWin95 )
   {
      MoveToEx( hDC, 1, wHeight - 2, &pt );
      LineTo( hDC, wWidth - 2, wHeight - 2 );
      MoveToEx( hDC, 2, wHeight - 3, &pt );
      LineTo( hDC, wWidth - 2, wHeight - 3 );
      MoveToEx( hDC, wWidth - 2, wHeight - 2, &pt );
      LineTo( hDC, wWidth - 2, FD_HEIGHT - 1 );
      MoveToEx( hDC, wWidth - 3, wHeight - 2, &pt );
      LineTo( hDC, wWidth - 3, FD_HEIGHT );
   }
   else
   {
      MoveToEx( hDC, 1, wHeight - 2, &pt );
      LineTo( hDC, wWidth - 2, wHeight - 2 );
      MoveToEx( hDC, wWidth - 2, wHeight - 2, &pt );
      LineTo( hDC, wWidth - 2, FD_HEIGHT - 1 );
   }

   for( w = 0; w < wPrompts; w++ )
   {
      if( ! bWin95 )
      {
         SETBLACKPEN( hDC );
         MoveToEx( hDC, wLeft, FD_HEIGHT - 1, &pt );
         LineTo( hDC, wLeft, 4 );
         LineTo( hDC, wLeft + 4, 0 );
         LineTo( hDC, wLeft + wSize - 3, 0 );
         LineTo( hDC, wLeft + wSize + 1, 4 );
         LineTo( hDC, wLeft + wSize + 1, FD_HEIGHT );
      }
      else
      {
         SETWHITEPEN( hDC );
         if( ( w + 1 != wOption + 1 ) )
         {
            MoveToEx( hDC, wLeft, FD_HEIGHT - 1, &pt );
            LineTo( hDC, wLeft, 3 + IF( w + 1 != wOption, 2, 0 ) );
            LineTo( hDC, wLeft + 2, 1 + IF( w + 1 != wOption, 2, 0 ) );
         }
         MoveToEx( hDC, wLeft + 2 - IF( ( w + 1 == wOption + 1 ), 2, 0 ),
                 1 + IF( w + 1 != wOption, 2, 0 ), &pt );
         LineTo( hDC, wLeft + wSize - 1 + IF( w + 1 == wOption - 1, 2, 0 ),
                 1 + IF( w + 1 != wOption, 2, 0 ) );
         // LineTo( hDC, wLeft + wSize + 1, 4 );
         // LineTo( hDC, wLeft + wSize + 1, FD_HEIGHT );
      }

      if( ! bWin95 )
      {
         SETWHITEPEN( hDC );
         MoveToEx( hDC, wLeft + 1, FD_HEIGHT - 1, &pt );
         LineTo( hDC, wLeft + 1, 4 );
         LineTo( hDC, wLeft + 4, 1 );
         LineTo( hDC, wLeft + wSize - 2, 1 );
      }

      if( ! bWin95 )
      {
         SelectObject( hDC, hDarkPen );
         MoveToEx( hDC, wLeft + wSize - 2, 2, &pt );
         LineTo( hDC, wLeft + wSize, 4 );
         LineTo( hDC, wLeft + wSize, FD_HEIGHT + 1 );
      }
      else
      {
         if( ( w + 1 != wOption - 1 ) || ( w + 1 == wOption ) )
         {
            SETBLACKPEN( hDC );
            MoveToEx( hDC, wLeft + wSize - 1, 2 + IF( w + 1 != wOption, 2, 0 ), &pt );
            LineTo( hDC, wLeft + wSize, 3 + IF( w + 1 != wOption, 2, 0 ) );
            LineTo( hDC, wLeft + wSize, FD_HEIGHT + 1 );

            SelectObject( hDC, hDarkPen );
            MoveToEx( hDC, wLeft + wSize - 1, 3 + IF( w + 1 != wOption, 2, 0 ), &pt );
            LineTo( hDC, wLeft + wSize - 1, FD_HEIGHT );
         }
      }

      if( w + 1 != wOption )
      {
         if( ! bWin95 )
         {
            SETBLACKPEN( hDC );
            MoveToEx( hDC, wLeft, FD_HEIGHT - 1, &pt );
            LineTo( hDC, wLeft + wSize + 1, FD_HEIGHT - 1 );
         }

         SETWHITEPEN( hDC );
         if( ! bWin95 )
         {
            MoveToEx( hDC, wLeft + ( w == 0 ), FD_HEIGHT, &pt );
            LineTo( hDC, wLeft + wSize + IF( ( w + 1 ) < wPrompts, 2, 0 ), FD_HEIGHT );
            MoveToEx( hDC, wLeft - 1 + IF( w == 0, 2, 0 ), FD_HEIGHT + 1, &pt );
            LineTo( hDC, wLeft + wSize + IF( ( w + 1 ) < wPrompts, 2, -1 ), FD_HEIGHT + 1 );
         }
         else
         {
            // MoveToEx( hDC, wLeft + ( w == 0 ), FD_HEIGHT );
            // LineTo( hDC, wLeft + wSize + IF( ( w + 1 ) < wPrompts, 2, 0 ), FD_HEIGHT );
            MoveToEx( hDC, wLeft - 1 + IF( w == 0, 2, 0 ), FD_HEIGHT, &pt );
            LineTo( hDC, wLeft + wSize + IF( ( w + 1 ) < wPrompts, 2, -1 ), FD_HEIGHT );
         }
         SelectObject( hDC, hFont1 );
      }
      else
      {
         if( ! bWin95 )
         {
            SETWHITEPEN( hDC );
            MoveToEx( hDC, wLeft + 1, FD_HEIGHT + 1, &pt );
            LineTo( hDC, wLeft + 1, FD_HEIGHT - 2 );
            MoveToEx( hDC, wLeft + 2, FD_HEIGHT + 1, &pt );
            LineTo( hDC, wLeft + 2, 4 );
            LineTo( hDC, wLeft + 3, 2 );
            LineTo( hDC, wLeft + wSize - 3, 2 );
            MoveToEx( hDC, wLeft + 3, 4, &pt );
            LineTo( hDC, wLeft + 5, 2 );
         }

         SelectObject( hDC, hDarkPen );
         if( ! bWin95 )
         {
            MoveToEx( hDC, wLeft + wSize - 3, 3, &pt );
            LineTo( hDC, wLeft + wSize - 1, 5 );
            MoveToEx( hDC, wLeft + wSize - 3, 2, &pt );
            LineTo( hDC, wLeft + wSize - 1, 4 );
            LineTo( hDC, wLeft + wSize - 1, FD_HEIGHT + 2 );
         }
         else
         {
            MoveToEx( hDC, wLeft + wSize - 1, 3, &pt );
            LineTo( hDC, wLeft + wSize - 1, FD_HEIGHT + 1 );
         }

         SelectObject( hDC, IF( ! bWin95, hFont2, hFont1 ) );
      }

      if( ! bWin95 )
      {
         rct.top    = 5;
         rct.left   = wLeft + FD_BORDER + 2;
         rct.bottom = FD_HEIGHT - 3;
         rct.right  = wLeft + wSize - FD_BORDER + 2;
      }
      else
      {
         rct.top    = 5 + IF( w + 1 != wOption, 2, 0 );
         rct.left   = wLeft + FD_BORDER + IF( bWin95, -1, 2 );
         rct.bottom = FD_HEIGHT - 3 + IF( w + 1 != wOption, 2, 0 );
         rct.right  = wLeft + wSize - FD_BORDER + 2;
      }

      if( _parl( 8, w + 1 ) )
        SetTextColor( hDC, CLR_BLACK );
      else
        SetTextColor( hDC, CLR_GRAY );

      SetBkColor( hDC, rgbPane );
      DrawText( hDC, _parc( 3, w + 1 ), _parclen( 3, w + 1 ),
                   &rct, DT_CENTER );

      wLeft += wSize + 1;

      if( bWin95 )
      {
         GetTextExtentPoint32( hDC, _parc( 3, w + 2 ), _parclen( 3, w + 2 ), &size );
         wSize = size.cx + 12;
      }

      if( ( wLeft + wSize + 2 ) > wWidth )
         wSize = wWidth - wLeft - 2;
   }

   SETWHITEPEN( hDC );
   if( ! bWin95 )
   {
      MoveToEx( hDC, wLeft - 1, FD_HEIGHT, &pt );
      LineTo( hDC, wWidth - 2, FD_HEIGHT );
      MoveToEx( hDC, wLeft - 2, FD_HEIGHT + 1, &pt );
      LineTo( hDC, wWidth - 3, FD_HEIGHT + 1 );
   }
   else
   {
      MoveToEx( hDC, wLeft - 1 - ( wOption != wPrompts ), FD_HEIGHT, &pt );
      LineTo( hDC, wWidth - 2, FD_HEIGHT );
   }

   if( ! bWin95 )
   {
      SETBLACKPEN( hDC );
      MoveToEx( hDC, wLeft, FD_HEIGHT - 1, &pt );
      LineTo( hDC, wWidth, FD_HEIGHT - 1 );
   }

   DeleteObject( hDarkPen );
   DeleteObject( hWhitePen );

   SetBkColor( hDC, rgbPane );
   ReleaseDC( hWnd, hDC );
}

//----------------------------------------------------------------------------//
